home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 23 / Amiga Format AFCD23 (Feb 1998, Issue 107).iso / -seriously_amiga- / shareware / programming / c / asyncio / src / readlineasync.c < prev    next >
C/C++ Source or Header  |  1997-12-01  |  1KB  |  77 lines

  1. #include "async.h"
  2.  
  3.  
  4. _LIBCALL LONG
  5. ReadLineAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buffer, _REG( d0 ) LONG bufSize )
  6. {
  7.     LONG    len;
  8.  
  9.     /* First read any data up to the LF or the buffer is full */
  10.     if( FGetsLenAsync( file, buffer, bufSize, &len ) )
  11.     {
  12.         UBYTE    *end;
  13.  
  14.         end = ( ( UBYTE * ) buffer ) + len - 1;
  15.  
  16.         if( *end != '\n' )
  17.         {
  18.             UBYTE    ch = 0;
  19.  
  20.             /* We didn't reach EOF yet */
  21.             while( TRUE )
  22.             {
  23.                 UBYTE    *ptr;
  24.                 LONG    i, count;
  25.  
  26.                 ptr = ( UBYTE * ) file->af_Offset;
  27.  
  28.                 if( count = file->af_BytesLeft )
  29.                 {
  30.                     /* Scan for LF char in buffer */
  31.                     for( i = 0; ( i < count ) && ( *ptr != '\n' ); ++i, ++ptr )
  32.                     {
  33.                     }
  34.  
  35.                     /* If i < count, then the loop above aborted
  36.                      * due to LF char.
  37.                      */
  38.                     if( i < count )
  39.                     {
  40.                         ch = '\n';
  41.                         ++i;
  42.                     }
  43.  
  44.                     file->af_BytesLeft -= i;
  45.                     file->af_Offset    += i;
  46.  
  47.                     if( i < count )
  48.                     {
  49.                         /* All done */
  50.                         break;
  51.                     }
  52.                 }
  53.  
  54.                 if( ReadAsync( file, &ch, 1 ) < 1 )
  55.                 {
  56.                     break;
  57.                 }
  58.  
  59.                 if( ch == '\n' )
  60.                 {
  61.                     /* All done */
  62.                     break;
  63.                 }
  64.             }
  65.  
  66.             if( ch == '\n' )
  67.             {
  68.                 /* Overwrite last char with LF */
  69.                 *end++ = '\n';
  70.                 *end = '\0';
  71.             }
  72.         }
  73.     }
  74.  
  75.     return( len );
  76. }
  77.